package framework.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;

import framework.controller.Controller;
import framework.controller.GameController;
import framework.model.GameModel;
import framework.model.Model;

public class AIview extends InteractiveView {

	private JButton stepButton;
	private JButton playStopButton;
	private JLabel label;
	
	public AIview(Model model, Controller controller) {
		super(model, controller);
		this.setFields();
		this.getModel().addListener(this);
		this.init();
	}
	
	private void setFields() {
		this.label = new JLabel("AI : ");
		this.stepButton = new JButton("STEP");
		this.stepButton.setActionCommand("step");
		this.stepButton.addActionListener(this);
		this.playStopButton = new JButton("OFF");
		this.playStopButton.setActionCommand("autoplay");
		this.playStopButton.addActionListener(this);
	};

	private void init() {
		//Panel
		super.setLayout(new GridLayout(1,3));
		super.add(label);
		super.add(stepButton);
		super.add(playStopButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()) {
			case "step" : 
				this.getController().handleStepRequest();
			break;
			case "autoplay_updated" : 
				this.playStopButton.setText(this.getModel().getAIRunning() ? "YES" : "NO");
			break;
			case "autoplay" : 
				this.getController().toggleAutoplay();
			break;
		};
	}
	
	@Override 
	public GameController getController() {
		return (GameController)super.getController();
	};
	
	@Override 
	public GameModel getModel() {
		return (GameModel)super.getModel();
	};

}
